package com.woniuxy.controller;

import com.woniuxy.entity.Address;
import com.woniuxy.entity.User;
import com.woniuxy.service.AddressService;
import com.woniuxy.units.MyCsvFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RestController
public class TestController {

    @Autowired
    private AddressService addressService;
    @RequestMapping("/createCsvFileJcTest")
    public void createCsvFileJcTest() {
        //类不确定 随便怎么传都行
        List<Address> addressList = addressService.queryByParentCodes("110100");
        //存放地址&文件名
        String fileName = "D:\\bttc\\"+ MyCsvFileUtil.buildCsvFileFileName(addressList);
        //创建表格行标题
        String tableNames = MyCsvFileUtil.buildCsvFileTableNames(addressList);
        //创建文件
        MyCsvFileUtil.writeFile(fileName, tableNames);
        //写入数据
        String contentBody = MyCsvFileUtil.buildCsvFileBodyMap(addressList);
        //调用方法生成
        MyCsvFileUtil.writeFile(fileName, contentBody);
    }

    @GetMapping("createUserInfo")
    public void writeFile(){
        ArrayList<User> users = new ArrayList<>();
        User user = new User();
        user.setUserId(1);
        user.setUserName("小金");
        user.setUserCode("King");
        user.setAge(28);

        User user2 = new User();
        user2.setUserId(2);
        user2.setUserName("小赞");
        user2.setUserCode("xiaozan");
        user2.setAge(18);
        User user3 = new User();
        user3.setUserId(3);
        user3.setUserName("小小音");
        user3.setUserCode("leekong");
        user3.setAge(22);
        users.add(user);
        users.add(user2);
        users.add(user3);

        //存放地址&文件名
        String fileName = "D:\\bttc\\"+ MyCsvFileUtil.buildCsvFileFileName(users);
        //创建表格行标题
//        String tableNames = MyCsvFileUtil.buildCsvFileTableNames(users);
        String tableNames = MyCsvFileUtil.buildCsvFileTableNamesNew( MyCsvFileUtil.resolveExcelTableName(users.get(0)));
        //创建文件
        MyCsvFileUtil.writeFile(fileName, tableNames);
        //写入数据
        String contentBody = MyCsvFileUtil.buildCsvFileBodyMap(users);
        //调用方法生成
        MyCsvFileUtil.writeFile(fileName, contentBody);
    }
}
